package com.zs.admin.service.wisdbook.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zs.admin.service.wisdbook.IWisdbookService;
import com.zs.admin.service.wisdbook.IWisdbookPageService;
import com.zs.admin.validate.wisdbook.WisdbookPageCreateValidate;
import com.zs.admin.validate.wisdbook.WisdbookPagePLUpdateValidate;
import com.zs.admin.validate.wisdbook.WisdbookPageSearchValidate;
import com.zs.admin.validate.wisdbook.WisdbookPageUpdateValidate;
import com.zs.admin.vo.wisdbook.WisdbookDetailVo;
import com.zs.admin.vo.wisdbook.WisdbookPageListedVo;
import com.zs.admin.vo.wisdbook.WisdbookPageDetailVo;
import com.zs.common.core.AjaxResult;
import com.zs.common.entity.wisdbook.WisdbookPage;
import com.zs.common.mapper.WisdbookPageMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 智书书页实现类
 * @author SwAdmin
 */
@Service
public class WisdbookPageServiceImpl implements IWisdbookPageService {
        
    @Resource
    WisdbookPageMapper wisdbookPageMapper;
    @Resource
    IWisdbookService iWisdbookService;

    /**
     * 智书书页列表
     *
     * @author SwAdmin
     * @param searchValidate 搜索参数
     * @return PageResult<WisdbookPageListedVo>
     */
    @Override
    public AjaxResult list( WisdbookPageSearchValidate searchValidate) {

        QueryWrapper<WisdbookPage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByAsc("page_num");
        //queryWrapper.orderByDesc("id");

        wisdbookPageMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:id:int",
                "=:bookId@book_id:int",
                "=:pageType@page_type:str",
        });
        List<WisdbookPage> iPage = wisdbookPageMapper.selectList(queryWrapper);

        List<WisdbookPageListedVo> list = new LinkedList<>();
        for(WisdbookPage item : iPage) {
            WisdbookPageListedVo vo = new WisdbookPageListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }
        //拿书的基本信息
        WisdbookDetailVo wisdbookDetailVo = iWisdbookService.detail(searchValidate.getBookId());
        wisdbookDetailVo.setPages(list);

        return AjaxResult.success(wisdbookDetailVo);
    }

    /**
     * 智书书页详情
     *
     * @author SwAdmin
     * @param id 主键参数
     * @return WisdbookPage
     */
    @Override
    public WisdbookPageDetailVo detail(Integer id) {
        WisdbookPage model = wisdbookPageMapper.selectOne(
                new QueryWrapper<WisdbookPage>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        WisdbookPageDetailVo vo = new WisdbookPageDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 智书书页新增
     *
     * @author SwAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(WisdbookPageCreateValidate createValidate) {
        WisdbookPage model = new WisdbookPage();
        model.setBookId(createValidate.getBookId());
        model.setPageName(createValidate.getPageName());
        model.setPageImg(createValidate.getPageImg());
        model.setPageNum(createValidate.getPageNum());
        model.setPageType(createValidate.getPageType());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        wisdbookPageMapper.insert(model);
    }

    /**
     * 智书书页编辑
     *
     * @author SwAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(WisdbookPageUpdateValidate updateValidate) {
        WisdbookPage model = wisdbookPageMapper.selectOne(
                new QueryWrapper<WisdbookPage>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setPageName(updateValidate.getPageName());
        model.setPageImg(updateValidate.getPageImg());
        model.setPageNum(updateValidate.getPageNum());
        model.setPageType(updateValidate.getPageType());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        wisdbookPageMapper.updateById(model);
    }

    /**
     * 智书书页删除
     *
     * @author SwAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        WisdbookPage model = wisdbookPageMapper.selectOne(
                new QueryWrapper<WisdbookPage>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        wisdbookPageMapper.updateById(model);
    }

    /**
     * 智书书页编辑
     *
     * @author SwAdmin
     * @param plUpdateValidate 参数
     */
    @Override
    public void pl_edit(WisdbookPagePLUpdateValidate plUpdateValidate) {
        for(WisdbookPageUpdateValidate validate : plUpdateValidate.getPages()){
            WisdbookPage model = wisdbookPageMapper.selectOne(
                    new QueryWrapper<WisdbookPage>()
                            .eq("id",  validate.getId())
                            .eq("is_delete", 0)
                            .last("limit 1"));

            if(model!=null) {
                model.setId(validate.getId());
                model.setPageNum(validate.getPageNum());
                model.setUpdateTime(System.currentTimeMillis() / 1000);
                wisdbookPageMapper.updateById(model);
            }
        }
    }
}
